package com.syaccp.service.impl;

import cn.hutool.db.sql.Order;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.syaccp.mapper.*;
import com.syaccp.model.*;
import com.syaccp.page.pageUtils;
import com.syaccp.request.TOrderMaterialRequest;
import com.syaccp.response.OrderDetailStatus;
import com.syaccp.service.TOrderDetailService;
import com.syaccp.utils.CheckWebPageUtil;
import com.syaccp.utils.DateUtils;
import com.syaccp.utils.OrderRelatedUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author DX
* @description 针对表【t_order_detail】的数据库操作Service实现
* @createDate 2023-07-25 09:12:24
*/
@Service
public class TOrderDetailServiceImpl extends ServiceImpl<TOrderDetailMapper, TOrderDetail> implements TOrderDetailService{

    @Autowired
    TOrderDetailMapper tOrderDetailMapper;

    @Autowired
    TOrderServiceImpl tOrderService;


    @Override
    public int queryInfoByConditionsNumber(TOrderMaterialRequest orderMaterialRequest) {
        return tOrderDetailMapper.queryInfoByConditionsNumber(orderMaterialRequest);
    }


    @Override
    public List<TOrderDetail> queryInfoByConditions(TOrderMaterialRequest orderMaterialRequest, pageUtils page) {
        return tOrderDetailMapper.queryInfoByConditions(orderMaterialRequest, page);
    }

    @Override
    public List<TOrderDetail> queryAuditions() {
        LambdaQueryWrapper<TOrderDetail> queryWrapper = Wrappers.lambdaQuery(TOrderDetail.class);
        queryWrapper.isNotNull(TOrderDetail::getZpshName);
        queryWrapper.groupBy(TOrderDetail ::getZpsh);
        return tOrderDetailMapper.selectList(queryWrapper);
    }

    @Override
    public List<TOrderDetail> queryModelNames() {
        LambdaQueryWrapper<TOrderDetail> queryWrapper = Wrappers.lambdaQuery(TOrderDetail.class);
        queryWrapper.groupBy(TOrderDetail ::getModelId);
        return tOrderDetailMapper.selectList(queryWrapper);
    }

    @Override
    public List<TOrderDetail> queryCompanyNames() {
        LambdaQueryWrapper<TOrderDetail> queryWrapper = Wrappers.lambdaQuery(TOrderDetail.class);
        queryWrapper.groupBy(TOrderDetail ::getCompanyId);
        return tOrderDetailMapper.selectList(queryWrapper);
    }

    @Override
    public Integer delInfoByIds(List<Integer> ids) {
        return tOrderDetailMapper.deleteBatchIds(ids);
    }

    @Override
    public Integer updateSelection(List<Integer> ids) {
        TOrderDetail torderDetail= new TOrderDetail();
        LambdaUpdateWrapper<TOrderDetail> updateWrapper= new LambdaUpdateWrapper<>();
        updateWrapper.in(TOrderDetail ::getId,ids);
        torderDetail.setJingpin(1);
        return tOrderDetailMapper.update(torderDetail,updateWrapper);
    }


    @Resource
    private TOrderDetailMapper orderDetailMapper;

    @Resource
    private SysDeptMapper sysDeptMapper;

    @Resource
    private TOrderMapper orderMapper;

    @Resource
    private DictMapper dictMapper;

    @Resource

    private TOrderCategoryMapper orderCategoryMapper;


    @Override
    public Integer addOrderDetail(TOrderDetail orderDetail) {


        return orderDetailMapper.insert(orderDetail);
    }

    @Override
    public TOrderDetail selectOneById(Integer id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",id);

        return orderDetailMapper.selectOne(queryWrapper);
    }

    @Override
    public Integer updateOrderDetail(TOrderDetail orderDetail,Integer id ) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",id);


        return  orderDetailMapper.update(orderDetail,queryWrapper);
    }

    @Override
    public List<Dict> QueryModelLevel() {
        QueryWrapper<Dict> qw=new QueryWrapper<>();
        qw.eq("type","model_level_name");
        List<Dict> dicts = dictMapper.selectList(qw);
        return dicts;
    }

    @Override
    public List<Dict> QueryStyleType() {
        QueryWrapper<Dict> qw=new QueryWrapper<>();
        qw.eq("type","product_style");
        List<Dict> dicts = dictMapper.selectList(qw);
        return dicts;
    }

    @Override
    public Integer UpdateTaskStatesByStaffId(Integer id) {
        LambdaUpdateWrapper<TOrderDetail> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(TOrderDetail::getMakeId, id)
                .and(aa->aa.eq(TOrderDetail::getStatus,1).or().eq(TOrderDetail::getStatus,2))
                .set(TOrderDetail::getStatus, 5);
        Integer rows = orderDetailMapper.update(null, lambdaUpdateWrapper);
        return rows;
    }

    @Override
    public Integer IsDelete(Integer id) {
        LambdaUpdateWrapper<TOrderDetail> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(TOrderDetail::getId, id).set(TOrderDetail::getDelFlag, 2);
        Integer rows = orderDetailMapper.update(null, lambdaUpdateWrapper);

        return rows;
    }

    @Override
    public List<TOrderCategory> QuerySceneTypeById(Integer id) {
        QueryWrapper<TOrderCategory> qw=new QueryWrapper<>();
        qw.eq("parent_id",id);
        List<TOrderCategory> orderCategories = orderCategoryMapper.selectList(qw);
        return orderCategories;
    }



    @Override
    public List<Dict> QueryAttachmentType(Dict dict) {
        QueryWrapper<Dict> qw=new QueryWrapper<>();
        qw.eq("type",dict.getType());
        List<Dict> dicts = dictMapper.selectList(qw);
        return dicts ;
    }

    @Override
    public PageInfo<TOrderDetail> QueryAllDetail(TOrderDetail orderDetail, int index) {
        PageHelper.startPage(index,20);
        return new PageInfo<>(orderDetailMapper.QueryAllDetail(orderDetail));
    }

    @Override
    public OrderDetailStatus QueryDetailsStatus() {

        OrderDetailStatus orderDetailStatus=new OrderDetailStatus(0,0,0,0,0,0,500);

        List<TOrderDetail> orderDetails = orderDetailMapper.QueryDetailsStatus();

        for (TOrderDetail orderDetail : orderDetails) {
            if (orderDetail.getStatus()==1){
                orderDetailStatus.setMakeCount(orderDetail.getCount());
            }else if (orderDetail.getStatus()==2){
                orderDetailStatus.setCompleteMinimap(orderDetail.getCount());
            }else if (orderDetail.getStatus()==3){
                orderDetailStatus.setWaiting(orderDetail.getCount());
            }else if (orderDetail.getStatus()==4){
                orderDetailStatus.setCancel(orderDetail.getCount());
            }else if (orderDetail.getStatus()==5){
                orderDetailStatus.setFinished(orderDetail.getCount());
            }
        }
        //总条数
        Integer totalNumber=orderDetailStatus.getMakeCount()+orderDetailStatus.getCompleteMinimap()
                +orderDetailStatus.getWaiting()+orderDetailStatus.getCancel()+orderDetailStatus.getFinished();
        orderDetailStatus.setTotalNumber(totalNumber);

        return orderDetailStatus;
    }

    @Override
    public List<SysDept> QueryCompanyMsg() {
        QueryWrapper<SysDept> qw=new QueryWrapper<>();
        qw.eq("parent_id",100);
        List<SysDept> sysDepts = sysDeptMapper.selectList(qw);
        return sysDepts;
    }

    @Override
    public TOrder QueryOrderNameById(Integer id) {
        QueryWrapper<TOrder> qw=new QueryWrapper<>();
        qw.eq("id",id);
        TOrder order = orderMapper.selectOne(qw);
        return order;
    }






    @Override
    public List<TOrderDetail> queryOrderDetailByOrderId(Integer orderId) {
        LambdaQueryWrapper<TOrderDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(TOrderDetail::getOrderId, orderId);
        return tOrderDetailMapper.selectList(lqw).stream().peek(item -> {
            if (StringUtils.isNotEmpty(item.getImage())) {
                if (!item.getImage().contains("127.0.0.1")) {
                    item.setImage(OrderRelatedUtil.setRealPicPath(item.getImage()));
                }
                if (!CheckWebPageUtil.checkUrlConnection(item.getImage())) {
                    item.setImage(OrderRelatedUtil.DEFAULT_PIC.getMessage());
                }
            }
        }).collect(Collectors.toList());
    }

    @Override
    public Integer insertOrderDetail(TOrderDetail orderDetail) {
        orderDetail.setCreateTime(DateUtils.getTodayDateObjDateTime())
                .setTaskNo(OrderRelatedUtil.getRandomNum(4))
                .setImage("http://127.0.0.1/uploadFiles/" + orderDetail.getImage());
        return tOrderDetailMapper.insert(orderDetail);
    }

    @Override
    public TOrderDetail queryOrderDetailById(Integer taskId) {
        return orderDetailMapper.selectById(taskId);
    }

    @Override
    public Integer updateOrderDetail(TOrderDetail orderDetail) {
        if (StringUtils.isNotBlank(orderDetail.getImage())) {
            orderDetail.setImage("http://127.0.0.1/uploadFiles/" + orderDetail.getImage());
        }
        return tOrderDetailMapper.updateById(orderDetail);
    }

    @Override
    public Integer deleteOrderDetail(Integer taskId) {
        return tOrderDetailMapper.deleteById(taskId);
    }

    @Override
    public Integer payMoney(TOrderDetail orderDetail) {
        LambdaUpdateWrapper<TOrderDetail> update = Wrappers.lambdaUpdate();
        update.setSql("paid_money=paid_money+" + orderDetail.getPayMoney() + ",payment=payment-" + orderDetail.getPayMoney())
                .eq(TOrderDetail::getId, orderDetail.getId());
        return tOrderDetailMapper.update(null, update);
    }
}




